﻿namespace WinRTClient.ViewModels
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows.Input;

    using dot_in.HTWKAidStation.Client;
    using dot_in.HTWKAidStation.Client.Mensa;
    using dot_in.HTWKAidStation.Client.Timetable;

    using StyleMVVM.ViewModel;

    using Windows.UI.Xaml.Controls;

    using WinRTClient.Views;

    public class MainPageViewModel : PageViewModel
    {
        private Course selectedCourse;

        private Faculty selectedFaculty;

        private ObservableCollection<Week> weeks;

        public MainPageViewModel()
        {
            this.Mensae = new ObservableCollection<Mensa>();
            this.LoadMensae();
            this.LoadTimetableData();
        }

        public ICommand ShowMenuCommand
        {
            get
            {
                return new DelegateCommand(_ => Navigation.Navigate(typeof(MenuWeekView), this.SelectedMensa));
            }
        }

        public ICommand ShowTimetableCommand
        {
            get
            {
                return new DelegateCommand(_ => Navigation.Navigate(typeof(TimetableView), new object[] { this.SelectedTutorialGroup, this.SelectedWeek }));
            }
        }

        public EventCommand FacultyClick
        {
            get
            {
                return new EventCommand((sender, args, parameter) => this.SelectedFaculty = (Faculty)((ItemClickEventArgs)args).ClickedItem);
            }
        }

        public ObservableCollection<Mensa> Mensae { get; set; }

        public Mensa SelectedMensa { get; set; }

        public ObservableCollection<Faculty> Faculties { get; set; }

        public Faculty SelectedFaculty
        {
            get
            {
                return this.selectedFaculty;
            }

            set
            {
                this.selectedFaculty = value;
                this.OnPropertyChanged();
            }
        }

        public Course SelectedCourse
        {
            get
            {
                return this.selectedCourse;
            }

            set
            {
                this.selectedCourse = value;
                this.OnPropertyChanged();
            }
        }

        public TutorialGroup SelectedTutorialGroup { get; set; }

        public ObservableCollection<Week> Weeks
        {
            get
            {
                return this.weeks;
            }

            set
            {
                this.weeks = value;
                this.OnPropertyChanged();
            }
        }

        public Week SelectedWeek { get; set; }

        private async void LoadMensae()
        {
            List<Mensa> items = await CachedCore.ParseMensa();
            foreach (Mensa item in items)
            {
                this.Mensae.Add(item);
            }
        }

        private async void LoadTimetableData()
        {
            this.Faculties = new ObservableCollection<Faculty>();
            List<Faculty> faculties = await CachedCore.ParseAllFaculties();
            foreach (Faculty faculty in faculties)
            {
                this.Faculties.Add(faculty);
            }

            this.Weeks = new ObservableCollection<Week>();
            foreach (Week week in await CachedCore.ParseAllWeeks())
            {
                this.Weeks.Add(week);
            }
        }
    }
}
